package com.jaya.budan.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jaya.budan.R;
import com.jaya.budan.adapter.VIP_ZX_HorizontalListViewAdapter;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.bean.VIP_JiLu;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.VolleyUtil;
import com.jaya.budan.views.HorizontalListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class GeRenTG_Buy_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back;
    private TextView tv_gerentg_buy_title;
    private ArrayList<VIP_JiLu> vip_jiLu = new ArrayList<>();
    private String access_token;
    private int mtype;
    private HorizontalListView main_hlv_vip;
    public static Activity activity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gerentg_buy);
        activity = this;
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        mtype = intent.getIntExtra("mtype", 0);
        back = findViewById(R.id.back);
        tv_gerentg_buy_title = findViewById(R.id.tv_gerentg_buy_title);
        main_hlv_vip = findViewById(R.id.main_hlv_vip);
        back.setOnClickListener(this);
        if (mtype == 1){
            tv_gerentg_buy_title.setText("个人推广");
        } else {
            tv_gerentg_buy_title.setText("动态推广");
        }
        postData();
        main_hlv_vip.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Button btn_dongtaitg_buy1 = view.findViewById(R.id.btn_dongtaitg_buy1);
                btn_dongtaitg_buy1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(GeRenTG_Buy_Activity.this, GeRenTG_Buy_2_Activity.class);
                        intent.putExtra("access_token", access_token);
                        intent.putExtra("vip_jiLu", vip_jiLu);
                        intent.putExtra("mtype", mtype);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_pop_gerentg_1:
//                ll_pop_gerentg.bringChildToFront(btn_pop_gerentg_1);//显示在上层，不被遮挡
//                ll_pop_gerentg.updateViewLayout(btn_pop_gerentg_1, btn_pop_gerentg_1.getLayoutParams());
//                btn_pop_gerentg_1.bringToFront();
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.gr_my_dongtai_DJ_Buy_Url + access_token + "&type=" + mtype, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("xinyun", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Gson gson = new Gson();
                        JSONArray info = jsonObject.getJSONArray("info");
                        vip_jiLu = gson.fromJson(info.toString(), new TypeToken<ArrayList<VIP_JiLu>>(){}.getType());
                        // 获取屏幕宽高
                        int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); // 屏幕宽（像素，如：480px）
                        main_hlv_vip.setAdapter(new VIP_ZX_HorizontalListViewAdapter(GeRenTG_Buy_Activity.this, screenWidth, null, null, 3, vip_jiLu, null));
                    } else {
                        ToolUtils.showToast(GeRenTG_Buy_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(GeRenTG_Buy_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
